using Godot;
using System;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.Intrinsics.X86;

public partial class Hero : Node2D
{
	public const float Speed = 300.0f;
	public const float JumpVelocity = -400.0f;

	private Vector2I _rightFlag = new Vector2I(1, 1);
	private Vector2I _leftFlag = new Vector2I(-1, 1);

	private Sprite2D heroSprite;

	private AnimationTree animationTree;

	private AnimationNodeStateMachinePlayback player;


	private int _walkToIdleFrame = 10;
	private int _frameCount = 0;

	private double physicsDelta = 0.0f;
	public override void _Ready()
	{
		base._Ready();
		heroSprite = GetNode<Sprite2D>("HeroSprite");
		animationTree = GetNode<AnimationTree>("AnimationTree");
		player = (AnimationNodeStateMachinePlayback)animationTree.Get("parameters/StateMachine/playback");
		player.Start("idle");
	}

	// Get the gravity from the project settings to be synced with RigidBody nodes.
	//public float gravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle(); 
	public override void _Input(InputEvent @event)
	{
		base._Input(@event);
		if (@event is InputEventKey eventKey)
		{
			this.pressAction(eventKey);
			this.releaseAction(eventKey);
		}
	}

	private void pressAction(InputEventKey eventKey)
	{
		if (eventKey.Pressed)
		{
			switch (eventKey.Keycode)
			{
				case Key.J:
					player.Travel("punch");
					break;
				case Key.Left:
					/*if (!heroSprite.FlipH)
					{
						heroSprite.FlipH = true;
					}*/
					if (Scale != _leftFlag)
					{
						Scale = _leftFlag;
					}

					break;
				case Key.Right:
					/*if (heroSprite.FlipH)
					{
						heroSprite.FlipH = false;
					}*/
					if (Scale != _rightFlag)
					{
						Scale = _rightFlag;
					}

					break;


			}

		}
	}

	private void releaseAction(InputEventKey eventKey)
	{
		if (eventKey.IsReleased())
		{
			GD.Print("我松开了,jlll", eventKey.Keycode);
			switch (eventKey.Keycode)
			{

			}
		}
	}

	public override void _PhysicsProcess(double delta)
	{



		Vector2 direction = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
		Vector2 vector = Vector2.Zero;
		if (direction != Vector2.Zero)
		{
			//velocity.X = direction.X * Speed;
			//_frameCount = _walkToIdleFrame;
			vector.X = direction.X * (float)(Speed * delta);
			vector.Y = direction.Y * (float)(Speed * delta);
			player.Travel("walk");
		}
		else
		{
			vector.X = Mathf.MoveToward(vector.X, 0, Speed);
			vector.Y = Mathf.MoveToward(vector.Y, 0, Speed);
		}
		Position += vector;

	}

	private void OnCollisionBody(Rid bodyRid, Node2D body, long bodyShapeIndex, long localShapeIndex)
	{
		GD.Print("Hero碰撞啦", bodyRid, " ", body.Name, "  ", localShapeIndex, bodyShapeIndex);

	}
}
